/*
@title: scrolling_maze
@description: "Scrolling Maze" is a large, scrolling maze game where the player navigates to reach the end. Unlike typical maze games, it features a limited field of view, offering a unique challenge as players explore its expansive layout.
@author: Ben
@tags: []
@addedOn: 2023-01-15
*/
/*
  Big maze. IDK. I don't really make mazes. 
  I also haven't ever used Javascript before

  I kinda just borrowed the Arch Linux logo.
  Check Arch Linux out at https://archlinux.org/

  I'm not affiliated with Arch Linux in any way, I just like it most days


  The lack of creativity in this project is due to the fact that this was mostly designed as a test for scrolling maps with collisions. it works.
*/
const player = "p";
const black_cube = "c";

setLegend(
  [player, bitmap`
................
................
.......77.......
......7777......
.......777......
.....77.777.....
.....777777.....
.....777777.....
....77777777....
....77777777....
...77777777.7...
...7777..777....
...777....777...
..77........77..
.77..........77.
................`],
  [black_cube, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
)

const themap = [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
                [1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1],
                [1,1,1,0,1,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1],
                [1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,0,1,0,1,1,1,1,0,1,0,0,1,1,1,1,1,0,0,1],
                [1,0,1,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,1,0,1,0,0,0,0,1,0,1,0,0,1,0,0,0,1,0,0,1],
                [1,0,1,0,0,0,0,0,0,0,1,1,1,1,1,1,0,1,0,0,0,0,0,0,1,0,1,0,1,1,1,1,0,1,0,1,0,0,1,1,1,0,1,0,0,1],
                [1,0,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,1],
                [1,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,0,1,1,1,1,1,1,0,1,0,1,0,0,0,0,1,0,1,0,0,1],
                [1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1,0,0,1],
                [1,0,1,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,0,1,1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1],
                [1,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,0,1],
                [1,0,1,1,1,1,0,1,0,1,0,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1],
                [1,0,0,0,0,0,0,1,0,1,0,1,1,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,0,1,0,0,0,1],
                [1,1,1,1,1,1,1,1,0,1,0,1,1,0,1,1,1,1,1,1,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1],
                [1,0,0,0,0,1,0,0,0,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1],
                [1,0,0,0,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0,1,1,1,1,1],
                [1,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,0,1],
                [1,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,1,0,1,0,0,0,1],
                [1,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,1,0,1,1,0,0,0,1,0,1,0,0,0,1],
                [1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1,0,0,1,1,1,0,1,0,0,0,1,0,0,0,1,1,1,0,0,0,1],
                [1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,0,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,1,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,0,1,0,0,0,1,1,1,1,1,1,0,1,1,1,1,0,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,1,1,1,0,1,1,1,1,1,0,0,0,0,1,0,1,0,0,1,0,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,1,0,0,1,0,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,1,1,1,1,1,1,0,1,0,0,1,0,0,0,0,0,0,1,0,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1,0,1,0,0,1,0,1,1,1,1,1,1,0,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1,0,1,1,1,1,0,0,0,0,0,0,0,0,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,1,1,1,1,1,1,0,0,0,0,1,0,1,1,1,1,1,1,0,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0,1,0,1,1,1,1,0,1,0,0,1,1,1,1,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,0,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,1,0,1,1,1,1,1,1,1,1,1,0,0,0,1],
                [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
                [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1],
                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0],
                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1],
                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0],];

// Step 1 - Add player to map
setMap(map`
.........
.........
.........
....p....
.........
.........
.........`)

const map_dimensions = [9, 7];
const map_center = [4, 3];

const xy_pos = [1, 2];

onInput("a", () => {
  if(!checkCollision("a")){
    xy_pos[0] += 1;
  }
});
onInput("d", () => {
  if(!checkCollision("d")){
    xy_pos[0] -= 1;
  }
});
onInput("w", () => {
  if(!checkCollision("w")){
    xy_pos[1] += 1;
  }
});
onInput("s", () => {
  if(!checkCollision("s")){
    xy_pos[1] -= 1;
  }
});

// Step 2 - Add move right controls
// END - PLAYER MOVEMENT CONTROLS

// Put obstacle in a random position
function spawnBlocks(x, y) {
  if(x >= 0 && y >= 0 && x < map_dimensions[0] && y < map_dimensions[1]){
    addSprite(x, y, black_cube);
  }
}

function clearBlocks(){
  let all_blocks = getAll();
    for(const block of all_blocks){
      if(block.type !== player){
        block.remove()
      }
    }
};

function mapRender(){
  clearBlocks();
  for(let i = 0; i < themap.length; i++){
    for(let j = 0; j < themap[i].length; j++){
      if(themap[i][j] === 1){
        spawnBlocks((xy_pos[0] + j), (xy_pos[1] + i));
      }
    }
  }
}

function newPosition(direction){
  let x_pos = map_center[0];
  let y_pos = map_center[1];
  switch(direction){
    case "w":
      y_pos -= 1;
      break;
    case "s":
      y_pos += 1;
      break;
    case "a":
      x_pos -= 1;
      break;
  case "d":
      x_pos += 1;
      break;
  default:
  }
  return [x_pos, y_pos];
}

function checkCollision(direction){
  let new_pos = newPosition(direction);
  let collision = false;
  let all_blocks = getAll();
    for(const block of all_blocks){
      if(block.type !== player){
        if(block.x === new_pos[0] && block.y === new_pos[1]){
          collision = true;
        }
      }
    }
  return collision;
}
mapRender();
afterInput(() => {
  mapRender();
  //spawnBlocks(xy_pos[0], xy_pos[1]);
});